import { ResultBean } from '@/api/role';

// 模拟角色数据
const mockRoles = [
  {
    roleId: 1,
    roleName: '超级管理员',
    roleLevel: 1,
    createTime: Date.now() - 86400000 * 30,
    updateTime: Date.now() - 86400000 * 5
  },
  {
    roleId: 2,
    roleName: '系统管理员',
    roleLevel: 2,
    createTime: Date.now() - 86400000 * 25,
    updateTime: Date.now() - 86400000 * 10
  },
  {
    roleId: 3,
    roleName: '部门管理员',
    roleLevel: 3,
    createTime: Date.now() - 86400000 * 20,
    updateTime: Date.now() - 86400000 * 15
  },
  {
    roleId: 4,
    roleName: '操作员',
    roleLevel: 4,
    createTime: Date.now() - 86400000 * 15,
    updateTime: Date.now() - 86400000 * 8
  },
  {
    roleId: 5,
    roleName: '普通用户',
    roleLevel: 5,
    createTime: Date.now() - 86400000 * 10,
    updateTime: Date.now() - 86400000 * 3
  }
];

// 模拟菜单树数据 - 匹配funcList结构
const mockMenuTree = [
  {
    funcId: 'f001',
    funcName: '系统管理',
    funcPath: '/system',
    upFuncId: null,
    index: 1,
    children: [
      {
        funcId: 'f002',
        funcName: '用户管理',
        funcPath: '/user/query',
        upFuncId: 'f001',
        index: 1,
        children: []
      },
      {
        funcId: 'f003',
        funcName: '角色管理',
        funcPath: '/role/query',
        upFuncId: 'f001',
        index: 2,
        children: []
      },
      {
        funcId: 'f004',
        funcName: '组织架构管理',
        funcPath: '/group/query',
        upFuncId: 'f001',
        index: 3,
        children: []
      }
    ]
  },
  {
    funcId: 'f010',
    funcName: '督察管理',
    funcPath: '/supervision',
    upFuncId: null,
    index: 2,
    children: [
      {
        funcId: 'f011',
        funcName: '督察任务管理',
        funcPath: '/supervision/query',
        upFuncId: 'f010',
        index: 1,
        children: []
      }
    ]
  },
  {
    funcId: 'f020',
    funcName: '仪表盘',
    funcPath: '/dashboard',
    upFuncId: null,
    index: 3,
    children: [
      {
        funcId: 'f021',
        funcName: '工作台',
        funcPath: '/dashboard/workplace',
        upFuncId: 'f020',
        index: 1,
        children: []
      },
      {
        funcId: 'f022',
        funcName: '监控台',
        funcPath: '/dashboard/monitor',
        upFuncId: 'f020',
        index: 2,
        children: []
      }
    ]
  }
];

// 角色菜单权限映射 - 使用funcId
const roleMenuMap = new Map<number, string[]>([
  [1, ['f001', 'f002', 'f003', 'f004', 'f010', 'f011', 'f020', 'f021', 'f022']], // 超级管理员 - 所有权限
  [2, ['f001', 'f002', 'f003', 'f020', 'f021', 'f022']], // 系统管理员 - 除组织架构管理外
  [3, ['f010', 'f011', 'f020', 'f021']], // 部门管理员 - 部分管理权限
  [4, ['f010', 'f011', 'f020', 'f021']], // 操作员 - 基础操作权限
  [5, ['f020', 'f021']] // 普通用户 - 仅查看权限
]);

let roleList = [...mockRoles];
let nextRoleId = 6;

// 创建成功响应
const createSuccessResponse = <T>(data: T, message = '操作成功'): ResultBean<T> => ({
  code: 200,
  message,
  data
});

// 创建错误响应
const createErrorResponse = (message = '操作失败'): ResultBean<null> => ({
  code: 500,
  message,
  data: null
});

export default [
  // 获取角色列表
  {
    url: '/api/role/query',
    method: 'post',
    response: ({ body }) => {
      const { pageNo = 1, pageSize = 10, roleName, roleLevel } = body;

      let filteredList = roleList;

      // 根据搜索条件过滤
      if (roleName) {
        filteredList = filteredList.filter(role => role.roleName.includes(roleName));
      }
      if (roleLevel) {
        filteredList = filteredList.filter(role => role.roleLevel === parseInt(roleLevel));
      }

      const start = (pageNo - 1) * pageSize;
      const end = start + pageSize;
      const list = filteredList.slice(start, end);

      return {
        code: 200,
        message: 'success',
        data: {
          rows: list,
          page: {
            totalRow: filteredList.length,
            pageNo: pageNo,
            pageSize: pageSize
          }
        }
      };
    }
  },

  // 根据ID获取角色
  {
    url: '/api/role/queryById',
    method: 'post',
    response: ({ body }) => {
      const roleId = typeof body === 'number' ? body : body.roleId;
      const role = roleList.find(r => r.roleId === parseInt(roleId));

      if (role) {
        // 获取角色的菜单权限
        const funcList = roleMenuMap.get(role.roleId) || [];
        return createSuccessResponse({
          ...role,
          funcList
        });
      } else {
        return createErrorResponse('角色不存在');
      }
    }
  },

  // 创建角色
  {
    url: '/role',
    method: 'post',
    response: ({ body }: any) => {
      const { roleName, roleLevel } = body;

      // 检查角色名称是否已存在
      if (roleList.some(r => r.roleName === roleName)) {
        return createErrorResponse('角色名称已存在');
      }

      const newRole = {
        roleId: nextRoleId++,
        roleName,
        roleLevel,
        createTime: Date.now(),
        updateTime: Date.now()
      };

      roleList.push(newRole);
      return createSuccessResponse(newRole, '创建成功');
    }
  },

  // 更新角色
  {
    url: '/role/:roleId',
    method: 'put',
    response: ({ params, body }: any) => {
      const { roleId } = params;
      const { roleName, roleLevel } = body;

      const roleIndex = roleList.findIndex(r => r.roleId === parseInt(roleId));

      if (roleIndex === -1) {
        return createErrorResponse('角色不存在');
      }

      // 检查角色名称是否已被其他角色使用
      if (roleList.some(r => r.roleName === roleName && r.roleId !== parseInt(roleId))) {
        return createErrorResponse('角色名称已存在');
      }

      const updatedRole = {
        ...roleList[roleIndex],
        roleName,
        roleLevel,
        updateTime: Date.now()
      };

      roleList[roleIndex] = updatedRole;
      return createSuccessResponse(updatedRole, '更新成功');
    }
  },

  // 删除角色
  {
    url: '/role/:roleId',
    method: 'delete',
    response: ({ params }: any) => {
      const { roleId } = params;
      const roleIndex = roleList.findIndex(r => r.roleId === parseInt(roleId));

      if (roleIndex === -1) {
        return createErrorResponse('角色不存在');
      }

      // 检查是否为系统默认角色
      if (parseInt(roleId) <= 5) {
        return createErrorResponse('系统默认角色不能删除');
      }

      roleList.splice(roleIndex, 1);
      return createSuccessResponse(null, '删除成功');
    }
  },

  // 批量删除角色
  {
    url: '/role/batch-delete',
    method: 'post',
    response: ({ body }: any) => {
      const { roleIds } = body;

      if (!Array.isArray(roleIds) || roleIds.length === 0) {
        return createErrorResponse('请选择要删除的角色');
      }

      // 过滤掉系统默认角色
      const deletableIds = roleIds.filter(id => id > 5);
      const deletedCount = deletableIds.length;

      roleList = roleList.filter(r => !deletableIds.includes(r.roleId));

      return createSuccessResponse(null, `成功删除 ${deletedCount} 个角色`);
    }
  },

  // 获取菜单树
  {
    url: '/menu/tree',
    method: 'get',
    response: () => {
      return createSuccessResponse(mockMenuTree);
    }
  },

  // 获取角色菜单权限
  {
    url: '/role/:roleId/menus',
    method: 'get',
    response: ({ params }: any) => {
      const { roleId } = params;
      const menuIds = roleMenuMap.get(parseInt(roleId)) || [];
      return createSuccessResponse(menuIds);
    }
  },

  // 更新角色菜单权限
  {
    url: '/role/:roleId/menus',
    method: 'post',
    response: ({ params, body }: any) => {
      const { roleId } = params;
      const { menuIds } = body;

      if (!Array.isArray(menuIds)) {
        return createErrorResponse('菜单ID列表格式错误');
      }

      // 更新角色菜单权限
      roleMenuMap.set(parseInt(roleId), menuIds);

      return createSuccessResponse(null, '权限设置成功');
    }
  },

  // 获取所有菜单功能
  {
    url: '/api/func/query',
    method: 'post',
    response: () => {
      return createSuccessResponse(mockMenuTree);
    }
  }
];
